import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 17655
 * Date: 2025-01-13
 * Time: 18:34
 */
public class test2 {
    /*
    LeetCode 290
     */
    public boolean wordPattern(String pattern, String ss) {
        String[] s = ss.split(" ");
        // 两个哈希表，相互映射
        Map<String,Character> hash1 = new HashMap<>();
        Map<Character,String> hash2 = new HashMap<>();

        // 长度
        if(pattern.length()!=s.length){
            return false;
        }

        for(int i =0;i<s.length;i++){
            char c = pattern.charAt(i);
            String word = s[i];
            // 判断映射
            if(hash1.containsKey(word)){
                if(!hash1.get(word).equals(c)){
                    return false;
                }
            }
            if(hash2.containsKey(c)){
                if(!hash2.get(c).equals(word)){
                    return false;
                }
            }
            hash1.put(word,c);
            hash2.put(c,word);
        }
        return true;
    }
}
